package jspeech.sample;

import jcommand.*;
import jcommand.event.*;

import jspeech.SpeechRecognizerListener;
import jspeech.exception.EngineConnectionException;
import jspeech.exception.EngineNotFoundException;

import java.awt.AWTException;
import java.awt.event.KeyEvent;

/**
 * Sample code for {@link jspeech.SpeechRecognizerListener}. This sample using
 * the {@link jspeech.SpeechRecognizerListener} object to execute the command.
 * All the commands are run in background. The developers need to defines all
 * commands before starting the engine. So, after the engine is started, the
 * developers can implemented other things in the main loop. The recognizer will
 * only running in a thread. <br>
 * <br>
 * <br>
 * <b>Commands implemented: </b><br>
 * <li>1) Internet - Open IE</li>
 * <li>2) Close - Close the active application (Pressing ALT+F4)</li>
 * <br>
 * <br>
 * 
 * @author Kenneth Lo
 * 
 */
public class SpeechRecognizerListenerDemo {

	public static void main(String[] args) {

		try {

			// Step 1: Create CommandSet object
			CommandSet commandSet = new CommandSet();

			// Step 1-1: Create Command (Launch Internet Explorer)
			Command c1 = new Command("Internet");
			c1.addAction(Action.Launch(Application.INTERNET_EXPLORER));

			// Step 1-2: Create Command (Press ALT+F4)
			Command c2 = new Command("Close");
			c2.addAction(Action
					.KeyHoldAndClick(KeyEvent.VK_ALT, KeyEvent.VK_F4));

			// Step 1-3: Add commands to command set
			commandSet.addCommand(c1);
			commandSet.addCommand(c2);

			// Step 2: Create SpeechRecognizerListener object with specific
			// commandset
			SpeechRecognizerListener sr = new SpeechRecognizerListener(
					commandSet, "JSpeech.xml");

			// Step 3: Start the listener
			sr.start();
		} catch (AWTException e1) {
			e1.printStackTrace();
		} catch (EngineConnectionException e1) {
			e1.printStackTrace();
		} catch (EngineNotFoundException e1) {
			e1.printStackTrace();
		}

		// Write your own program
		try {
			while (true) {
				Thread.sleep(10000);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
